/*
 * crash_plot.cpp
 *
 *  Created on: 20.07.2013
 *      Author: SpirTanol
 */

#include "ui/crash_plot.h"
#include "global.h"
#include "packets/crash_oscillogram_packet.h"

// QWT
#include <qwt_scale_draw.h>
#include <qwt_legend.h>
#include <qwt_legend_item.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_picker.h>
#include <qwt_scale_widget.h>
#include <qwt_scale_engine.h>

// QT
#include <QFontMetrics>
#include <QDateTime>
#include <QDebug>
//=========================================================================
class Picker : public QwtPlotPicker
{
	QwtPlot *oPlot;
public:
	Picker(int x, int y, int flags, RubberBand rb, DisplayMode dm, QwtPlotCanvas *c, QwtPlot *plt) : QwtPlotPicker(x, y, flags, rb, dm, c)
	{
		oPlot = plt;
	}
	~Picker() {}

	QwtText trackerText(const QPoint &pos) const
	{
		int time = oPlot->invTransform(QwtPlot::xBottom, pos.x());
		QString str = QString::fromUtf8("Ток: ") + QString::number(oPlot->invTransform(QwtPlot::yLeft, pos.y()), 'f', 1) +
				QString::fromUtf8(" Напряжение: ") + QString::number(oPlot->invTransform(QwtPlot::yRight, pos.y()), 'f', 1) + '\n' +
				QString::number(time) + QString::fromUtf8("мс");
		return str;
	}
};
//=========================================================================
CrashPlot::CrashPlot() : QwtPlot()
{
	init();
}

CrashPlot::CrashPlot(const CrashPlot *plot)
{
	init();

	oMinCurrent = plot->oMinCurrent;

	oTimes = plot->oTimes;
	for (int i = 0; i < 6; ++i)
		oCurves[i].oData = plot->oCurves[i].oData;

	for (int i = 0; i < 6; ++i)
		oCurves[i].oCurve->setData(oTimes, oCurves[i].oData);

	replot();
}

CrashPlot::~CrashPlot()
{

}
//=========================================================================
void CrashPlot::init()
{
	setAutoReplot(false);
	plotLayout()->setAlignCanvasToScales(true);
	QwtLegend *legend = new QwtLegend();
	legend->setItemMode(QwtLegend::CheckableItem);
	insertLegend(legend, QwtPlot::RightLegend);
	setMinimumWidth(360);
	setCanvasBackground(QColor(240, 241, 239));
	enableAxis(QwtPlot::yRight);
	axisScaleEngine(QwtPlot::yLeft)->setAttribute(QwtScaleEngine::Symmetric);
	axisScaleEngine(QwtPlot::yRight)->setAttribute(QwtScaleEngine::Symmetric);

	QwtText text(trUtf8("Ток (А)"));
	text.setFont(QFont("Arial", 8));
	setAxisTitle(QwtPlot::yLeft, text);

	text.setText(trUtf8("Напряжение (В)"));
	setAxisTitle(QwtPlot::yRight, text);
	enableAxis(QwtPlot::yRight);

	text.setText(QString::fromUtf8("Время (мс)"));
	setAxisTitle(QwtPlot::xBottom, text);

	QwtPlotCurve *curve;
	//Токи
	curve = new QwtPlotCurve(trUtf8("Ток A"));
	curve->setPen(QColor("#FF9933"));
	curve->attach(this);
	oCurves[C_CURRENT_A].oCurve = curve;

	curve = new QwtPlotCurve(trUtf8("Ток B"));
	curve->setPen(QColor("#339933"));
	curve->setZ(curve->z() - 1);
	curve->attach(this);
	oCurves[C_CURRENT_B].oCurve = curve;

	curve = new QwtPlotCurve(trUtf8("Ток C"));
	curve->setPen(QColor("#FF0000"));
	curve->setZ(curve->z() - 2);
	curve->attach(this);
	oCurves[C_CURRENT_C].oCurve = curve;
	//Напряжение
	curve = new QwtPlotCurve(trUtf8("Напряжение A"));
	curve->setPen(QColor("#FF99FF"));
	curve->attach(this);
	curve->setYAxis(QwtPlot::yRight);
	curve->setZ(curve->z() - 3);
	oCurves[C_VOLTAGE_A].oCurve = curve;

	curve = new QwtPlotCurve(trUtf8("Напряжение B"));
	curve->setPen(QColor("#3399FF"));
	curve->setYAxis(QwtPlot::yRight);
	curve->setZ(curve->z() - 4);
	curve->attach(this);
	oCurves[C_VOLTAGE_B].oCurve = curve;

	curve = new QwtPlotCurve(trUtf8("Напряжение C"));
	curve->setPen(QColor("#FF00FF"));
	curve->setYAxis(QwtPlot::yRight);
	curve->setZ(curve->z() - 5);
	curve->attach(this);
	oCurves[C_VOLTAGE_C].oCurve = curve;

	showCurve(oCurves[C_CURRENT_A].oCurve, true);
	showCurve(oCurves[C_CURRENT_B].oCurve, true);
	showCurve(oCurves[C_CURRENT_C].oCurve, true);
	showCurve(oCurves[C_VOLTAGE_A].oCurve, true);
	showCurve(oCurves[C_VOLTAGE_B].oCurve, true);
	showCurve(oCurves[C_VOLTAGE_C].oCurve, true);

	connect(this, SIGNAL(legendChecked(QwtPlotItem *, bool)), this, SLOT(showCurve(QwtPlotItem *, bool)));

	QwtPlotGrid *grid = new QwtPlotGrid;
	grid->enableY(true);
	grid->enableYMin(true);
	grid->enableX(true);
	grid->enableXMin(false);
	grid->setMajPen(QPen(Qt::blue, 0, Qt::DotLine));
	grid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
	grid->attach(this);

	new Picker(QwtPlot::xBottom, QwtPlot::yLeft,
	QwtPicker::PointSelection,
	QwtPicker::CrossRubberBand, QwtPicker::AlwaysOn, canvas(), this);

	oTimes.reserve(65);
	oTimes.push_back(-30.0);
	for (int i = 64; i >= 1; --i)
		oTimes.push_back(-0.4171875 * i);
	setAxisScale(QwtPlot::xBottom, -26.7, oTimes.back());

	/*for (int i = 0; i <= 64; ++i)
		oTimes.push_back(-0.4171875 * i);
	oTimes.push_back(-30.0);
	setAxisScale(QwtPlot::xBottom, oTimes.front(), -26.7);*/
}
//=========================================================================
void CrashPlot::updateData(const CrashOscillogramPacket *data)
{
	for (int i = 0; i < 6; ++i)
		oCurves[i].oData.clear();
	double coeffCur = data->coeffCur();
	double coeffVol = data->coeffVol();

	for (int i = 0; i < 64; ++i)
	{
		oCurves[C_CURRENT_A].oData.push_back(double(data->curPhaseA(i)) * coeffCur);
		oCurves[C_CURRENT_B].oData.push_back(double(data->curPhaseB(i)) * coeffCur);
		oCurves[C_CURRENT_C].oData.push_back(double(data->curPhaseC(i)) * coeffCur);

		oCurves[C_VOLTAGE_A].oData.push_back(double(data->volPhaseA(i)) * coeffVol);
		oCurves[C_VOLTAGE_B].oData.push_back(double(data->volPhaseB(i)) * coeffVol);
		oCurves[C_VOLTAGE_C].oData.push_back(double(data->volPhaseC(i)) * coeffVol);
	}

	for (int i = 0; i < 6; ++i)
	{
		oCurves[i].oData.push_front(oMinCurrent);
		oCurves[i].oCurve->setData(oTimes, oCurves[i].oData);
	}

	replot();
}
//=========================================================================
void CrashPlot::showCurve(QwtPlotItem *item, bool on)
{
	item->setVisible(on);
	QWidget *w = legend()->find(item);
	if ( w && w->inherits("QwtLegendItem") )
		((QwtLegendItem *)w)->setChecked(on);
	replot();
}
